//
//  OrderReplyViewController.m
//  xpkc
//
//  Created by 胡志飞 on 15/12/11.
//  Copyright © 2015年 TopSage. All rights reserved.
//

#import "OrderReplyViewController.h"
#import "UserMessage.h"
@interface OrderReplyViewController ()<UITextViewDelegate>
{
    
    UITextView *oneTextView ;
}
@end

@implementation OrderReplyViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.navigationItem.title = @"评价页";
        [self.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                 [UIColor colorWithRed:255/255.0f green:159/255.f blue:0/255.f alpha:1], NSForegroundColorAttributeName,
                                                 nil] forState:UIControlStateSelected];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self createNavigationView];
    
    [self createView];
    
    UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/4, SCREEN_HEIGHT-50-68, SCREEN_WIDTH/2, 50)];
    button.backgroundColor=[UIColor redColor];
    [button setTitle:@"提交评价" forState:(UIControlStateNormal)];
    [button setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    button.titleLabel.font=[UIFont boldSystemFontOfSize:19];
    [self.view addSubview:button];
    [button addTarget:self action:@selector(replyCommit) forControlEvents:(UIControlEventTouchUpInside)];
    
    
   }



/**
 提交评价
 */
-(void)replyCommit{
    
    NSString *module = @"courseBuy";
    NSString *interface = @"docoursecomment";
    
    NSString *timeString = [GetTime getTime];
    NSString *hashString = [[NSString stringWithFormat:@"%@/%@/key/%@/time/%@",module,interface,KEY,timeString] md5];
    NSString *urlString = [NSString stringWithFormat:@"%@/laravel/public/%@/%@/hash/%@/appid/1",URL2,module,interface,hashString];
    
    NSDictionary *parameter = @{@"time":timeString,@"mid":[UserMessage sharedUserMessage].userMid,@"parent_id":@0,@"username":[UserMessage sharedUserMessage].userId,@"aid":self.aid,@"content":oneTextView.text};
    
    //POST请求
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:urlString parameters:parameter success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        if ([[dic objectForKey:@"succ"] isEqualToNumber:@1]) {
            ShowAlertMsg([dic objectForKey:@"info"]);
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            ShowAlertMsg(@"评论失败");
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        ShowAlertMsg(@"评论失败");
    }];

}

-(void)createView{
    
    CGFloat offwidth = 0.04 * SCREEN_WIDTH;
    
    CGFloat scrollH = 0;
    
    UIScrollView * scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 20, SCREEN_WIDTH, SCREEN_HEIGHT-64-60-40)];
    
    [self.view addSubview:scrollView];
    
    
    UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(offwidth * 4, 0, SCREEN_WIDTH - 8 * offwidth, 150)];
    
    imageView.image=[UIImage imageNamed:@"abc"];
    
    [scrollView addSubview:imageView];
    scrollH += 150 + 10;
    

    
    scrollH += 20 + 50;
    
    
    
    oneTextView = [[UITextView alloc] init];
    oneTextView.frame = CGRectMake(offwidth, scrollH, SCREEN_WIDTH - 2 * offwidth, SCREEN_HEIGHT - scrollH - 60);
    
    oneTextView.backgroundColor = [UIColor whiteColor];
    oneTextView.alpha = 1.0;
    oneTextView.text = @"请给出您的宝贵评价";
    oneTextView.textAlignment = NSTextAlignmentLeft;
    oneTextView.font = [UIFont boldSystemFontOfSize:15.5f]; // 设置字体大小
    oneTextView.textColor = [UIColor grayColor];
    [oneTextView setEditable:YES];
    oneTextView.dataDetectorTypes = UIDataDetectorTypeAll;
    oneTextView.keyboardType = UIKeyboardTypeDefault;
    oneTextView.returnKeyType = UIReturnKeySearch;
    oneTextView.scrollEnabled = YES;
    oneTextView.delegate = self;
    [scrollView addSubview:oneTextView];
 
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(UIKeyboardWillShowNotification:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(UIKeyboardWillHideNotification:) name:UIKeyboardWillHideNotification object:nil];
    
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([@"\n" isEqualToString:text])
    {
        [textView resignFirstResponder];
        return NO;
    }
    
    return YES;
}



-(void)UIKeyboardWillHideNotification:(NSNotification *)sender{
    
    self.view.transform=CGAffineTransformIdentity;
    
}
-(void)UIKeyboardWillShowNotification:(NSNotification *)sender{
    CGRect rect=[[sender.userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey]CGRectValue];
    CGFloat height= rect.size.height;
    self.view.transform=CGAffineTransformMakeTranslation(0, -height);
    
}
-(void)textViewDidBeginEditing:(UITextView *)textView
{
    oneTextView.text=@"";
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [oneTextView resignFirstResponder];
}


-(void)createNavigationView{
    
    if (IsIOS7) {
        [self setEdgesForExtendedLayout:UIRectEdgeNone];
    }
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
